﻿namespace CatClient.Message.Internals
{
    using CatClient.Message;
    using CatClient.Message.Spi.Codec;
    using System;
    using System.Runtime.CompilerServices;

    public class DefaultMessageTree : IMessageTree
    {
        private string _mMessageId;
        private string _mParentMessageId;
        private string _mRootMessageId;

        public IMessageTree Copy() => 
            new DefaultMessageTree { 
                Domain = this.Domain,
                HostName = this.HostName,
                IpAddress = this.IpAddress,
                MessageId = this._mMessageId,
                ParentMessageId = this._mParentMessageId,
                RootMessageId = this._mRootMessageId,
                SessionToken = this.SessionToken,
                ThreadGroupName = this.ThreadGroupName,
                ThreadId = this.ThreadId,
                ThreadName = this.ThreadName,
                Message = this.Message
            };

        public override string ToString()
        {
            ChannelBuffer buf = new ChannelBuffer(0x2000);
            new PlainTextMessageCodec().Encode(this, buf);
            buf.Reset();
            buf.Skip(4);
            return buf.ToString();
        }

        public string Domain { get; set; }

        public string HostName { get; set; }

        public string IpAddress { get; set; }

        public IMessage Message { get; set; }

        public string MessageId
        {
            get => 
                this._mMessageId;
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    this._mMessageId = value;
                }
            }
        }

        public string ParentMessageId
        {
            get => 
                this._mParentMessageId;
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    this._mParentMessageId = value;
                }
            }
        }

        public string RootMessageId
        {
            get => 
                this._mRootMessageId;
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    this._mRootMessageId = value;
                }
            }
        }

        public string SessionToken { get; set; }

        public string ThreadGroupName { get; set; }

        public string ThreadId { get; set; }

        public string ThreadName { get; set; }
    }
}

